package org.example.leetcode.core.controller;


import org.example.leetcode.codeExecutor.api.CodeExecutorService;
import org.example.leetcode.common.domain.CodeDO;
import org.example.leetcode.common.domain.CodeResult;
import org.example.leetcode.common.pojo.GlobalResponse;
import org.example.leetcode.common.pojo.PageCond;
import org.example.leetcode.common.pojo.PageResult;
import org.example.leetcode.core.api.TQuestionPublicService;
import org.example.leetcode.core.annotation.RecommendAnnotation;
import org.example.leetcode.core.dao.TQuestionMapper;
import org.example.leetcode.core.dao.TQuestionTypeRelationMapper;
import org.example.leetcode.core.domain.TQuestion;
import org.example.leetcode.core.domain.TQuestionTypeRelation;
import org.example.leetcode.core.dto.*;
import org.example.leetcode.core.service.TQuestionService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhc
 * @since 2024-02-25
 */
@RestController
@RequestMapping("/t-question")
public class TQuestionController {
    @Resource
    TQuestionService tQuestionService;
    @Resource
    CodeExecutorService codeExecutorService;
    @Resource
    TQuestionMapper tQuestionMapper;
    @Resource
    TQuestionTypeRelationMapper tQuestionTypeRelationMapper;

    @Resource
    TQuestionPublicService tQuestionPublicService;
    @PostMapping("codeExecute")
    public GlobalResponse<CodeResult> codeExecute(@RequestBody CodeDO codeDO){
        CodeResult codeResult = tQuestionService.executeCode(codeDO);
        return GlobalResponse.success(codeResult);
    }
    @PostMapping("/page")
    @RecommendAnnotation
    public GlobalResponse<PageResult<QuestionPageVO>> page(@RequestBody QuestionPageCond questionPageCond){
        return GlobalResponse.success(tQuestionService.page(questionPageCond));
    }
    @GetMapping("/recommend")
    @RecommendAnnotation
    public GlobalResponse<List<QuestionPageVO>> page(@RequestParam("number")String limit){
        return GlobalResponse.success(tQuestionService.queryRecommend(Integer.valueOf(limit)));
    }
    @GetMapping("/recommend/item")
    public GlobalResponse<List<QuestionPageVO>> recommendItem(@RequestParam("number") String limit){
        return GlobalResponse.success(tQuestionService.queryRecommendItem(Integer.valueOf(limit)));
    }

    @GetMapping("/generate")
    public void generate(){
        List<TQuestion> tQuestions = tQuestionMapper.selectList(null);
        for(TQuestion tQuestion:tQuestions){
            String type = tQuestion.getType();
            String[] split = type.split(",");
            for(String s:split){
                if(StringUtils.isEmpty(s.trim())){
                    continue;
                }
                TQuestionTypeRelation tQuestionTypeRelation = new TQuestionTypeRelation();
                tQuestionTypeRelation.setQuestionId(tQuestion.getId());
                tQuestionTypeRelation.setTypeId(Long.valueOf(s.trim()));
                tQuestionTypeRelationMapper.insert(tQuestionTypeRelation);
            }
        }
    }
    @GetMapping("/info/{titleSlug}")
    public GlobalResponse<TQuestion> getQuestionInfo(@PathVariable("titleSlug")String titleSlug){
        TQuestion res=tQuestionService.getQuestionInfo(titleSlug);
        return GlobalResponse.success(res);
    }

    @PostMapping("/manager/add")
    public GlobalResponse<Boolean> add(@RequestBody TQuestionAddDto tQuestionAddDto){
        tQuestionService.add(tQuestionAddDto);
        return GlobalResponse.success(true);
    }
    @GetMapping("/manager/info/{titleSlug}")
    public GlobalResponse<TQuestionAddDto> getInfo(@PathVariable("titleSlug")String titleSlug){
        return GlobalResponse.success(tQuestionService.getManagerQuestionInfo(titleSlug));
    }
    @DeleteMapping("/{titleSlug}")
    public GlobalResponse<Boolean> delete(@PathVariable("titleSlug")String titleSlug){
        tQuestionService.delete(titleSlug);
        return GlobalResponse.success(true);
    }

    @PostMapping("/hot")
    public GlobalResponse<PageResult<DailyHotQuestion>> getHot(@RequestBody PageCond pageCond){
        return GlobalResponse.success(tQuestionService.getHot(pageCond));
    }
    @GetMapping("/dailyQuestionGenerate")
    public GlobalResponse<Boolean> dailyQuestionGenerate(@RequestParam("date")String date){
        tQuestionPublicService.dailyQuestionGenerate(date);
        return GlobalResponse.success(Boolean.TRUE);
    }
    @GetMapping("/dailyQuestion")
    public GlobalResponse<List<DailyQuestionVo>> getCurrentQuestion(){
        return GlobalResponse.success(tQuestionService.getCurrentQuestion());
    }
    @GetMapping("/questionLog")
    public GlobalResponse<UserQuestionLogVo> queryQuestionLog(@RequestParam("title")String title,@RequestParam(value = "language",required = false)String language){
        return GlobalResponse.success(tQuestionService.queryQuestionLog(title,language));
    }
    @GetMapping("/freeMarker")
    public ModelAndView test(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("description","给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target  的那 两个 整数，并返回它们的数组下标。\n" +
                "\n" +
                "你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。\n" +
                "\n" +
                "你可以按任意顺序返回答案");
        Example[] examples=new Example[3];
        String[] suggestions=new String[3];
        for(int i=0;i<3;i++){
            examples[i]=new Example();
            examples[i].setInput("nihao");
            examples[i].setInput("output");
            examples[i].setImageUrl("https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg");
            suggestions[i]="提示"+i;
        }
        modelAndView.addObject("examples",examples);
        modelAndView.addObject("suggestions",suggestions);
        modelAndView.addObject("extraInfo","你可以想出一个时间复杂度小于 O(n2) 的算法吗？");
        return modelAndView;
    }

}

